<template>
  <div>
    <!-- 年、月 -->
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="small" style="width:120px" @change="dateChange">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="currentMouth" size="small" style="width:120px;margin-left:10px" @change="dateChange">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item">{{ item }}</el-option>
      </el-select>
    </el-row>
    <!-- 放置一个日历组件 -->
    <el-calendar v-model="currentDate">
      <template v-slot:dateCell="{ date, data }">
        <div class="date-content">
          <span class="text">{{ data.day | getDayFilters }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    getDayFilters(value) {
      // value格式：   2021-12-09
      var arr = value.split('-') // split() 方法用于把一个字符串分割成字符串数组。
      return arr[2]
    }
  },
  props: {
    startDate: {
      type: Date,
      // 这里要用函数形式得到当前时间 不能直接 new Date()
      default: () => new Date() // 有传递值就用传递的值，没有就默认使用当前时间 new Date()
    }
  },
  data() {
    return {
      yearList: [], // 要遍历的年
      currentYear: null, // 当前年份
      currentMouth: null, // 当前月份
      currentDate: null
    }
  },
  created() {
    this.currentYear = this.startDate.getFullYear() // 当前年份
    this.currentMouth = this.startDate.getMonth() + 1 // 当前月份
    // 快速生成数组的方法
    // var arr = Array.from(Array(11), (v, i) => 10 + i - 5)
    // 结果：arr = [5,6,7,8,9,10,11,12,13,14,15]
    this.yearList = Array.from(Array(11), (v, i) => this.currentYear + i - 5)
    this.dateChange()
  },
  methods: {
    dateChange() {
      this.currentDate = new Date(`${this.currentYear}-${this.currentMouth}-1`)
      console.log('啥比东西')
    },
    isWeek(a) {
      return a.getDay() === 6 || a.getDay() === 0
    }
  }
}
</script>

<style scoped >
 /deep/ .el-calendar-day {
  height:  auto;
 }
 /deep/ .el-calendar-table__row td,/deep/ .el-calendar-table tr td:first-child,  /deep/ .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 /deep/ .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 /deep/ .el-calendar__header {
   display: none
 }
</style>
